/*
 * SearchController.js
 * 
 * Description: The searchString accepts a string from a calling method. It loads the Reading and Glossary XML files, searching for the term.
 * If it finds the term, it adds a clip of the surrounding words to an array (surroundingWords).
 * It then looks for the next occurance of the <sectionnumber> tag, extracts its information, and adds it to an array (sectionsLocated).
 * Once there are no more occurances, it returns the sectionsLocated array. The surroundingWords array could be called as needed.
 * 
 * Preconditions: Glossary.xml and Reading.xml files must be formatted to provide text locations using a <sectionnumber> tag.
 * The caller must provide an input string.
 * 
 * Postconditions: The SearchController will return an array containing the section location information.
 * This information can then be used to create a link back to the correct section.
 * 
 * Last update by Charles Ackerman.
 * 
 * Notes: Untested, but posted for any input/comments. Some basic code functions may help here.
 */

var searchString; //The string entered by the user.

//The search results variables store the results of the search.
//Using the same key on both will access the corresponding data.
//E.g., glossarySearchResultText[1] will return "Apple" and glossarySearchResultSection Apple's section, "1.1".
var glossarySearchResultsText = new Array();
var glossarySearchResultsSection = new Array();

var readingSearchResultsText = new Array();
var readingSearchResultsSection = new Array();

//Search the Glossary
//Accepts a searchString
//Returns 

function searchGlossary(searchString)
{
	
	//Load XML for Glossary
	//Code modified from examples used on w3schools.com.
	
	xmlGlossary=new XMLHttpRequest();
	xmlGlossary.open("GET","glossary.xml",false);
	xmlGlossary.send();								//Executes the previous XML commands
	
	//Search functionality modified from answer on Stack Exchange
	//URL: http://stackoverflow.com/questions/17016359/search-xml-with-javascript-and-display-results-in-table

    size = searchString.length; //Determines the size of the search string
    
    /* A working test to prevent null search terms should go here; this may or may not be it.
    if (input == null || input == "")
    {
        return false;
    }
	*/

    for (i=0;i<x.length;i++) //Search through the various tags.
    {
        startString = searchString.substring(0,size);
        //if (startString.toLowerCase() == input.toLowerCase())
        //{
        	readingSearchResultsText[searchIndex] = xmlGlossary.getElementsByTagName("term")[i].childNodes[0].nodeValue;
        	
        	if (glossarySearchResultsText[i] != null)
        		{
        			glossarySearchResultsSection[searchIndex] = xmlGlossary.getElementsByTagName("sectionnumber")[i].childNodes[0].nodeValue;
        			       	searchIndex = searchIndex + 1;
	        	}
            
            //divText = "<h1>The contact details are:</h1><br /><table border=1><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Street</th><th>City</th><th>State</th><th>Postcode</th></tr>" + "<tr><td>" + firstname + "</td><td>" + lastname + "</td><td>" + phone + "</td><td>" + street + "</td><td>" + city + "</td><td>" + state + "</td><td>" + postcode + "</td></tr>" + "</table>";
            //break;
        //}
        /*else
        {
            divText = "<h2>The contact does not exist.</h2>";
        }*/
    }
    //document.getElementById("results").innerHTML= divText;
	
	//Glossary XML loaded into object "xmlGlossary".
	//Search for the search string inside of the xmlGlossary
	//This may be helpful: http://stackoverflow.com/questions/17016359/search-xml-with-javascript-and-display-results-in-table
	
	
	
	/* Recommended/pulled from the w3schools.com
	document.getElementById("to").innerHTML=
	xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
	document.getElementById("from").innerHTML=
	xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
	document.getElementById("message").innerHTML=
	xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
	*/
}

//Search the Reading, basically the same as before with slight changes.
function searchReading(searchString)
{
	
	//Load XML for Glossary
	//Code modified from examples used on w3schools.com.
	
	xmlReading=new XMLHttpRequest();
	xmlReading.open("GET","glossary.xml",false);
	xmlReading.send();								//Executes the previous XML commands
	
	//Search functionality modified from answer on Stack Exchange
	//URL: http://stackoverflow.com/questions/17016359/search-xml-with-javascript-and-display-results-in-table

    size = searchString.length; //Determines the size of the search string
    
    /* A working test to prevent null search terms should go here; this may or may not be it.
    if (input == null || input == "")
    {
        return false;
    }
	*/

    for (i=0;i<x.length;i++) //Search through the various tags.
    {
        startString = searchString.substring(0,size);
        //if (startString.toLowerCase() == input.toLowerCase())
        //{
        	readingSearchResultsText[searchIndex] = xmlReading.getElementsByTagName("text")[i].childNodes[0].nodeValue;
        	
        	if (readingSearchResultsText[i] != null)
        		{
        			readingSearchResultsSection[searchIndex] = xmlReadng.getElementsByTagName("sectionnumber")[i].childNodes[0].nodeValue;
        			       	searchIndex = searchIndex + 1;
	        	}
            
            //divText = "<h1>The contact details are:</h1><br /><table border=1><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Street</th><th>City</th><th>State</th><th>Postcode</th></tr>" + "<tr><td>" + firstname + "</td><td>" + lastname + "</td><td>" + phone + "</td><td>" + street + "</td><td>" + city + "</td><td>" + state + "</td><td>" + postcode + "</td></tr>" + "</table>";
            //break;
        //}
        /*else
        {
            divText = "<h2>The contact does not exist.</h2>";
        }*/
    }
    document.getElementById("results").innerHTML= divText;
	
	//Glossary XML loaded into object "xmlGlossary".
	//Search for the search string inside of the xmlGlossary
	//This may be helpful: http://stackoverflow.com/questions/17016359/search-xml-with-javascript-and-display-results-in-table
	
	
	
	/* Recommended/pulled from the w3schools.com
	document.getElementById("to").innerHTML=
	xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
	document.getElementById("from").innerHTML=
	xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
	document.getElementById("message").innerHTML=
	xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
	*/
}

//Accept the search string and search the glossary and then the reading.
function search(searchString)
{
	searchGlossary(searchString);
	searchReading(searchString);
}
